package com.ziq.zcamp.post

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.NetworkUtils
import com.gmlive.camp.data.exception.LocalException
import com.ziq.base.extension.launchWithTryAsyncGlobal
import com.ziq.base.extension.launchWithTryAsynchronous
import com.ziq.zcamp.R
import com.ziq.zcamp.api.repository.DefaultPostTopicsRepository
import com.ziq.zcamp.api.repository.PostTopicsRepository
import com.ziq.zcamp.api.request.camp.PlaceDetailPunchRecommendParam
import com.ziq.zcamp.api.request.post.PostingDeleteParam
import com.ziq.zcamp.api.request.post.PostingDetailParam
import com.ziq.zcamp.api.request.post.PostingRecordParam
import com.ziq.zcamp.api.respond.CampBaseRespond
import com.ziq.zcamp.api.respond.post.PostingDetailRespond
import com.ziq.zcamp.api.service.CampService
import com.ziq.zcamp.api.service.NetWorkException
import com.ziq.zcamp.api.service.PostingService
import com.ziq.zcamp.api.service.ServiceModule.Companion.onApiFail
import com.ziq.zcamp.api.service.request
import com.ziq.zcamp.bean.post.ImageInfo
import com.ziq.zcamp.bean.post.PostingDetail
import com.ziq.zcamp.bean.topics.TopicsDetailsInfo
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.manager.UserManager
import com.ziq.zcamp.view.StatusView
import com.ziq.zcamp.view.StatusViewData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

@HiltViewModel
class DynamicDetailViewModel @Inject constructor() : ViewModel() {

    @Inject
    lateinit var mPostingService: PostingService

    val dimiss = MutableLiveData<Boolean>()
    val postingDetail = MutableLiveData<PostingDetail>()
    val statusShow = MutableLiveData<StatusViewData>()

    fun requestDetail(id: String, uid: String,
                      onSuccess: ((result: PostingDetailRespond) -> Unit)? = {
                          statusShow.postValue(StatusViewData(StatusView.IDLE))
                          postingDetail.postValue(it.data)
                          it.data?.let {item ->
                              val tempLike = UserManager.instance.postingLike.value ?: mutableMapOf<String, UserManager.PostingLikeStatus>()
                              tempLike[item.post?.post_id?:""] = UserManager.PostingLikeStatus(item.post?.like_nums?:0, item.post?.is_like?:false)
                              UserManager.instance.postingLike.postValue(tempLike)
                          }
                      },
                      onFail: ((msg:String , code : Int) -> Unit)? = { msg,code->
                          if(code == 1101){
                              onApiFail(msg)
                              dimiss.postValue(true)
                          } else {
                              if(NetworkUtils.isConnected()){
                                  onApiFail(msg)
                                  statusShow.postValue(StatusViewData(StatusView.NO_DATA))
                              } else {
                                  onApiFail( "无网络")
                                  statusShow.postValue(StatusViewData(StatusView.NET_ERROR))
                              }
                          }

                      }) {
        statusShow.postValue(StatusViewData(StatusView.LOADING))
        launchWithTryAsynchronous(whenTry = {
            val respond = mPostingService.postingDetail(PostingDetailParam().apply {
                this.post_id = id
                this.create_uid = uid
            }).request()


            respond.data?.parseData()

            withContext(Dispatchers.Main){
                onSuccess?.invoke(respond)
            }
        }, whenCatch = {
            withContext(Dispatchers.Main){
                when(it){
                    is NetWorkException ->{
                        onFail?.invoke(it.errorMsg,it.errorCode)
                    }
                    is LocalException -> {
                        onFail?.invoke(GlobalContextManager.getString(R.string.all_net_error),it.errorCode)
                    }
                    else -> {
                        onFail?.invoke(GlobalContextManager.getString(R.string.all_net_error),-1)
                    }
                }
            }
        })
    }

    fun delDetail(id: String, uid: String,
                  onSuccess: ((result: CampBaseRespond) -> Unit)? = {},
                  onFail: ((msg: String) -> Unit)? = {
                      onApiFail(it)
                  }) {
        launchWithTryAsynchronous(whenTry = {
            val respond = mPostingService.postingDelete(PostingDeleteParam().apply {
                this.post_id = id
                this.create_uid = uid
            }).request()

            withContext(Dispatchers.Main){
                onSuccess?.invoke(respond)
            }
        }, whenCatch = {
            withContext(Dispatchers.Main){
                when(it){
                    is NetWorkException ->{
                        onFail?.invoke(it.errorMsg)
                    }
                    else -> {
                        onFail?.invoke(GlobalContextManager.getString(R.string.all_net_error))
                    }
                }
            }
        })
    }


    private val mRepository : PostTopicsRepository by lazy {
        DefaultPostTopicsRepository()
    }

    fun requestTopicsDetail(topicsName :String, onSuccess: ((result: TopicsDetailsInfo) -> Unit) = {}) {
        launchWithTryAsynchronous(whenTry = {
            val info = mRepository.topicDetails(topicsName)
            withContext(Dispatchers.Main){
                onSuccess.invoke(info)
            }
        }, whenCatch = {
            withContext(Dispatchers.Main){
                when(it){
                    is LocalException -> {
                        onApiFail(GlobalContextManager.getString(R.string.all_net_error))
                    }
                    is NetWorkException -> {
                        onApiFail(it.message?:"")
                    }
                    else -> {
                        onApiFail(GlobalContextManager.getString(R.string.all_net_error))
                    }
                }
            }
        })
    }

}